#include "TankStatusFuncs.h"
#include "easylogging++.h"
#include <string>
TankStatusFuncs tank_status(_mem_name);

#ifndef InitEASYLOG
#define InitEASYLOG
    INITIALIZE_EASYLOGGINGPP //日志功能初始化、静态变量初始化，所以必须放在主函数之外
#endif
// 设置日志系统
static void SetEasyLoggingConf(void)
{
    // 使用默认配置  
    el::Configurations myLoggingConf;  
    myLoggingConf.setToDefault();
    myLoggingConf.setGlobally(el::ConfigurationType::ToFile, "true");
    myLoggingConf.setGlobally(el::ConfigurationType::PerformanceTracking, "false");
    myLoggingConf.set(el::Level::Trace, el::ConfigurationType::Enabled, "false");
    myLoggingConf.set(el::Level::Debug, el::ConfigurationType::Filename, "false");
    myLoggingConf.set(el::Level::Error, el::ConfigurationType::Filename, "myLog_%datetime{%Y%M%d_%H%m%s_%g}_error.log");
    myLoggingConf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "false");//禁用
    myLoggingConf.set(el::Level::Warning, el::ConfigurationType::Enabled, "false");//禁用
    // myLoggingConf.set(el::Level::Warning, el::ConfigurationType::Filename, "dataLog/myLog_%datetime{%Y%M%d_%H%m%s_%g}_warning.log");
    myLoggingConf.set(el::Level::Info, el::ConfigurationType::Filename, "myLog_%datetime{%Y%M%d_%H%m%s_%g}_info.log");
    myLoggingConf.set(el::Level::Verbose, el::ConfigurationType::Enabled, "false");//禁用
    el::Loggers::reconfigureAllLoggers(myLoggingConf);
	// LOG(DEBUG)   << "***** debug log  *****";
	LOG(ERROR)   << "***** error log  *****";
	LOG(INFO)    << "***** info log  *****";
}

int main(int argc, char* argv[])
{
    SetEasyLoggingConf();//初始化日志配置
    if (argc == 1 || ( argc == 2 && std::string(argv[1]) == std::string("read"))) // 无默认参数
    {
        std::shared_ptr<TankStatus> _stat = std::make_shared<TankStatus>(TankStatus::TANK_UNSET);
        tank_status.ReadTankStatusByMem(_stat);
        LOG(INFO) << "Read Status is : "<< (int) (*_stat);
    }
    else if (argc == 2 && std::string(argv[1]) == std::string("write"))
    {
        LOG(ERROR) << "write need param, repeat it !";
    }
    else if (argc == 2 && std::string(argv[1]) == std::string("toggle"))
    {
        std::shared_ptr<TankStatus> _stat = std::make_shared<TankStatus>(TankStatus::TANK_UNSET);
        tank_status.ReadTankStatusByMem(_stat);
        LOG(INFO) << "need to toggle : " << _mem_name << " , now the value is " << (int) (*_stat);
        tank_status.toggleSystemStart();
        LOG(INFO) << "toggled!";
    }
    else if (argc == 3 && std::string(argv[1]) == std::string("write"))
    {
        std::shared_ptr<TankStatus> _stat = std::make_shared<TankStatus>(TankStatus::TANK_UNSET);
        try
        {
            *_stat = (TankStatus)std::stoi(std::string(argv[2]));
            LOG(INFO) << "need to write to " << _mem_name << "is " << (int)*_stat;
        }
        catch(const std::exception& e)
        {
            LOG(ERROR) << "cann't translate your param to tankstatus!";
            std::cerr << e.what() << '\n';
        }
        tank_status.WriteTankStatusToMem(_stat);
        LOG(INFO) << "writed it ! ";
    }
    else
    {
        LOG(ERROR) << "unsurport param , repeat!";
    }
    return 0;
}